package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.transaction.util.MrchTransParam;
import com.turing.post.tuxedo.util.ICheckAcc;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OCheckAcc;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "PCPS.SVC9099")
@Remote( { LocalService.class, PCPSService.class })
@Local
public class CheckAcc extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate ICheckAcc
	 * 
	 * @return ICheckAcc
	 */
	public ICheckAcc makeRequestObject() {
		return new ICheckAcc();
	}

	/**
	 * CheckAcc 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * CheckAcc service entry 对帐交易的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		ICheckAcc itr = (ICheckAcc) req;
		OCheckAcc otr = (OCheckAcc) ack;

		MrchTransParam imtp = setMrchTransParam(req);

		imtp.debitCount = itr.debitCount;
		imtp.debitAmount = itr.debitAmount;
		imtp.creditCount = itr.creditCount;
		imtp.creditAmount = itr.creditAmount;

		imtp = mrchBusinessImpl.CheckAccount(imtp);

		// 主机流水号
		otr.tranTime = itr.curSysDateTime;
		otr.respCode = TuxedoRespCode.RC_SUCCESS;
	}

	/**
	 * CheckAcc 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OCheckAcc());
	}
}